
var categoryDAL = require('../DAL/categoryDAL');
var productDAL=require('../DAL/productDAL');
var async = require('async');
var randomHelper=require('../util/randomHelper');


function indexController(app) {
	/* GET home page. */
	//app.get方法第一个参数是我们添加网站可以请求的地址
	//首页
	app.get('/', function(req, res) {

		//应用async模块的api:parallel处理并行无关联操作
		async.parallel({ //控制流程1:多级菜单加载
			categorylist: function(done) {
				//处理逻辑
				categoryDAL.getCategoryAll(function(catelist) {
					//循环构造数组对象模型
					var categorylist = create_objectArrayList(catelist, 0);

					done(null, categorylist);
				});

			},//控制流程2:新品发布前五项
            newgoodsTop5: function(done) {
                //处理逻辑
                productDAL.getGoodsTopbyNum(5,function(newtoplist){
                    done(null,newtoplist);
                });
            },
            userInfo:function(done){
            	var userInfo=req.session.customer;
            	done(null,userInfo);
            },//智能净化板块
            jinghualist:function(done){
            	productDAL.getTopNumGoodlistByCategoryId(37,2,function(jinghualist){
            		done(null,jinghualist);
            	});
            },//洗衣机板块
            xiyijilist:function(done){
            	productDAL.getTopNumGoodlistByCategoryId(48,2,function(xiyijilist){            		
            		done(null,xiyijilist);
            	});
            },//女装热卖四项
            nvzhuanglist:function(done){
            	productDAL.getTopNumGoodlistByCategoryId(41,4,function(nvzhuanglist){            		
            		done(null,nvzhuanglist);
            	});
            }
		}, function(err, results) {           
            
			res.render('index', {
				categorylist: results.categorylist,
                newgoodsTop5:results.newgoodsTop5,
                jinghualist:results.jinghualist,
                xiyijilist:results.xiyijilist,
                nvzhuanglist:results.nvzhuanglist,
                userInfo:results.userInfo
			});
			
		});

	});
	
	

	//递归构造树型数组对象
	function create_objectArrayList(data, pid) {
		pid = pid || 0;
		var list = [];
		data.map(function(item) {
			if(item.up_id == pid) {
				item.list = create_objectArrayList(data, item.category_Id);
				list.push(item);
			}
		});
		return list;
	}

	

	//产品页面
	app.get('/product', function(req, res) {
		//res.render方法的第一个参数是我们请求地址对应的网页文件名
		res.render('product');
	});

	//验证码
	app.get('/code', function(req, res) {
		var img = randomHelper.makeCapcha(req); 
        
        //console.log(req.session.CAPTCHA_KEY);     
		res.setHeader('Content-Type', 'image/bmp');
		res.send(img.getFileData());
	});

	//活动页面
	app.get('/discount/1111',function(req,res){
		//应用async模块的api:parallel处理并行无关联操作
		async.parallel({ //控制流程1:多级菜单加载
			categorylist: function(done) {
				//处理逻辑
				categoryDAL.getCategoryAll(function(catelist) {
					//循环构造数组对象模型
					var categorylist = create_objectArrayList(catelist, 0);

					done(null, categorylist);
				});

			},
            userInfo:function(done){
            	var userInfo=req.session.customer;
            	done(null,userInfo);
            }
		}, function(err, results) {           
            
			res.render('discount', {
				categorylist: results.categorylist,
                userInfo:results.userInfo
			});
			
		});
	});
	
	//品牌优选
	app.get('/ppyx',function(req,res){
		//应用async模块的api:parallel处理并行无关联操作
		async.parallel({ //控制流程1:多级菜单加载
			categorylist: function(done) {
				//处理逻辑
				categoryDAL.getCategoryAll(function(catelist) {
					//循环构造数组对象模型
					var categorylist = create_objectArrayList(catelist, 0);

					done(null, categorylist);
				});

			},
            userInfo:function(done){
            	var userInfo=req.session.customer;
            	done(null,userInfo);
            }
		}, function(err, results) {           
            
			res.render('ppyx', {
				categorylist: results.categorylist,
                userInfo:results.userInfo
			});
			
		});
	});

	//测试模块
	app.get('/service/service_online',function(req,res){
		res.render('service/service_online');
	});
}


module.exports = indexController;